{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 线性模型"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 最小二乘法\n",
    "#### 预测房价"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    面积     价格\n",
      "0   56   7800\n",
      "1  104   9000\n",
      "2  156   9200\n",
      "3  200  10000\n",
      "4  250  11000\n",
      "5  300  12000\n",
      "[[170]]\n",
      "回归系数:[[16.32229076]]\n",
      "截距:[6933.4063421]\n",
      "预测值:[[9708.19577086]]\n"
     ]
    }
   ],
   "source": [
    "from sklearn import linear_model\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "data = [[56,7800],[104,9000],[156,9200],[200,10000],[250,11000],[300,12000]]\n",
    "columns = ['面积','价格']\n",
    "df = pd.DataFrame(data=data,columns=columns)\n",
    "x=pd.DataFrame(df['面积'])\n",
    "y=pd.DataFrame(df['价格'])\n",
    "print(df)\n",
    "clf = linear_model.LinearRegression()\n",
    "clf.fit(x,y) # 拟合线性模型\n",
    "k=clf.coef_ # 回归系数\n",
    "b=clf.intercept_ # 截距\n",
    "x0 = np.array([[170]])\n",
    "print(x0)\n",
    "# 通过给定的x0给出预测\n",
    "y0 = clf.predict(x0)\n",
    "print(f\"回归系数:{k}\")\n",
    "print(f\"截距:{b}\")\n",
    "print(f\"预测值:{y0}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 使用岭回归预测房价"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    面积     房价\n",
      "0   56   7800\n",
      "1  104   9000\n",
      "2  156   9200\n",
      "3  200  10000\n",
      "4  250  11000\n",
      "5  300  12000\n",
      "[[170]]\n",
      "回归系数:[[16.32189646]]\n",
      "截距;[6933.47639485]\n",
      "预测值:[[9708.19879377]]\n"
     ]
    }
   ],
   "source": [
    "from sklearn.linear_model import Ridge\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "data = [[56,7800],[104,9000],[156,9200],[200,10000],[250,11000],[300,12000]]\n",
    "df = pd.DataFrame(data=data,columns=['面积','房价'])\n",
    "print(df)\n",
    "x = pd.DataFrame(df['面积'])\n",
    "y = pd.DataFrame(df['房价'])\n",
    "clf = Ridge(alpha=1)\n",
    "clf.fit(x,y)\n",
    "k=clf.coef_ # 回归系数\n",
    "b=clf.intercept_ # 截距\n",
    "x0 = np.array([[170]])\n",
    "print(x0)\n",
    "y0 = clf.predict(x0)\n",
    "print(f\"回归系数:{k}\")\n",
    "print(f\"截距;{b}\")\n",
    "print(f\"预测值:{y0}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "bost shape:\n",
      "(506, 13)\n"
     ]
    }
   ],
   "source": [
    "# 预测波士顿房价\n",
    "from sklearn.datasets import load_boston\n",
    "from sklearn.model_selection import train_test_split\n",
    "boston = load_boston()\n",
    "print(f\"bost shape:\\n{boston.data.shape}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "特征名:['CRIM' 'ZN' 'INDUS' 'CHAS' 'NOX' 'RM' 'AGE' 'DIS' 'RAD' 'TAX' 'PTRATIO'\n",
      " 'B' 'LSTAT']\n"
     ]
    }
   ],
   "source": [
    "print(f\"特征名:{boston.feature_names}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "训练集分数:0.77\n",
      "测试集分数:0.64\n"
     ]
    }
   ],
   "source": [
    "from sklearn.linear_model import LinearRegression\n",
    "# 划分数据集\n",
    "X_train,X_test,y_train,y_test = train_test_split(boston.data,boston.target,random_state=0)\n",
    "# 初始化最小二乘法模型\n",
    "clf1 = LinearRegression()\n",
    "# 训练模型\n",
    "clf1.fit(X_train,y_train)\n",
    "print(f\"训练集分数:{round(clf1.score(X_train,y_train),2)}\")\n",
    "print(f\"测试集分数:{round(clf1.score(X_test,y_test),2)}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "训练集分数:0.77\n",
      "测试集分数:0.63\n"
     ]
    }
   ],
   "source": [
    "# 初始化岭回归模型\n",
    "clf2 = Ridge(alpha=0.1)\n",
    "clf2.fit(X_train,y_train)\n",
    "print(f\"训练集分数:{round(clf2.score(X_train,y_train),2)}\")\n",
    "print(f\"测试集分数:{round(clf2.score(X_test,y_test),2)}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
